from flask import render_template,request
from app import db
from . import admin_user
from .model import User
import json


@admin_user.route('/list')
def list():
    return render_template("pages/admin/user/list.html")


@admin_user.route('/add',methods=['GET','POST'])
def add():
    if request.method == 'POST':
        user_pk = request.form.get('user_pk', None)
        user_loginname = request.form.get('user_loginname', None)
        user_name = request.form.get('user_name', None)
        user_sex = request.form.get('user_sex', None)
        user_idcard = request.form.get('user_idcard', None)
        user_phone = request.form.get('user_phone', None)
        user_birthday = request.form.get('user_birthday', None)
        user_email = request.form.get('user_email', None)
        user_intime = request.form.get('user_intime', None)

        newobj = User()
        newobj.user_pk=user_pk
        newobj.user_loginname = user_loginname
        newobj.user_name = user_name
        newobj.user_sex = user_sex
        newobj.user_idcard = user_idcard
        newobj.user_phone = user_phone
        newobj.user_birthday = user_birthday
        newobj.user_email = user_email
        newobj.user_intime = user_intime
        db.session.add(newobj)
        db.session.commit()
    users = User.query.all()
    return json.dumps(users)


@admin_user.route('/select')
def select():
    users = User.query.all()
    print(users)
    return json.dumps(users)
    # fail_data = {
    #     'state': 'fail',
    #     'msg':  '用户名或密码不正确'
    # }
    # return json.dumps(fail_data)